/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#include "display_task.h"

#include <q3frame.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qobject.h>
#include <qtimer.h>
//Added by qt3to4:
#include <QShowEvent>
#include <Q3BoxLayout>
#include <Q3HBoxLayout>
#include <Q3VBoxLayout>
#include "atc.h"
#include "presenter.h"


using namespace pact;


////////////////////////////////////////////////////////////////////////////////////////////////////
//
// pact::DisplayTask
//
//--------------------------------------------------------------------------------------------------
//
//

DisplayTask::DisplayTask( DisplayTaskBuilder* b ) 
	: Task()
{
	//
	// add builders widget
	//

	Q3BoxLayout *vmgr = new Q3VBoxLayout( this );
	vmgr->addStretch();
	Q3BoxLayout *hmgr = new Q3HBoxLayout( vmgr );
	vmgr->addStretch();

	hmgr->addStretch();
	QWidget* widget = b->_widget;
	widget->reparent( this, 0, QPoint() );
	hmgr->addWidget( widget );
	hmgr->addStretch();

	//
	// connect any count down timers
	//

	foreach(CountDown* countdown, findChildren<CountDown*>()) {
		connect( countdown, SIGNAL( sig_timeout() ), this, SLOT( terminate() ) );
		connect( this, SIGNAL( sig_shown() ), countdown, SLOT( start() ) );
	}
}


/*!
 *
 */
DisplayTask::~DisplayTask() {}

//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
void DisplayTask::showEvent( QShowEvent *e ) {
	Task::showEvent(e);
	emit sig_shown();
}



////////////////////////////////////////////////////////////////////////////////////////////////////
//
// pact::DisplayTaskBuilder
//
//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
DisplayTaskBuilder::DisplayTaskBuilder() 
		:_widget( new Q3Frame(NULL) ) //todo: initialize with real parent component?
		,_layout( new Q3VBoxLayout(_widget, 20) )
{
}

/*!
 *
 */
DisplayTaskBuilder::~DisplayTaskBuilder()  {}


//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
DisplayTaskBuilder* DisplayTaskBuilder::set_text( const std::string  &text ) {
	QLabel *label = new QLabel(text.c_str(), _widget);
	label->setAlignment( Qt::AlignCenter );
	_layout->addWidget( label );
	return this;
}

/*!
 *
 */
DisplayTaskBuilder* DisplayTaskBuilder::set_link( const std::string  &link ) {
	Presenter *p = pact::Presenter::instance();
	GlobalVariables vars = p->globals();
	GlobalVariables::iterator vit = vars.find( link );
	if ( vit != vars.end() ) {
			QLabel *label = new QLabel( QString("%1").arg(vit->second->value()), _widget );
			label->setAlignment( Qt::AlignCenter );
			label->setPaletteForegroundColor( Qt::darkGreen );
			_layout->addWidget( label ); 
	}
	return this;
}

/*!
 *
 */
DisplayTaskBuilder* DisplayTaskBuilder::set_countdown( int time ) {
	_layout->addWidget( new CountDown( time, _widget ) );
	return this;
}


//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
DisplayTask* DisplayTaskBuilder::build() {
	return new DisplayTask(this);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//
// pact::CountDown
//
//--------------------------------------------------------------------------------------------------
//
//

const QString CountDown::FORMAT_TEXT( "%02d:%02d:%02d" );

//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
CountDown::CountDown( long secs, QWidget *parent ) 
		:QLabel(parent) 
		,_timer( new QTimer(this) )
		,_secs( secs )
{
	setAlignment( Qt::AlignCenter );
	setPaletteForegroundColor( Qt::red );
	update_display();
	connect( _timer, SIGNAL( timeout() ), this, SLOT( update_time() ) );
}

/*!
 *
 */
CountDown::~CountDown() {}


//--------------------------------------------------------------------------------------------------
// [public] slots
//

/*!
 *
 */
void CountDown::start() {
	_timer->start( atc::ONE_SECOND );
}

/*!
 *
 */
void CountDown::update_time() {
	--_secs;
	update_display();
	if ( !_secs ) emit sig_timeout();
}


//--------------------------------------------------------------------------------------------------
//
//

/*!
 *
 */
void CountDown::update_display() {
	int s = _secs % atc::SIXTY;
	int m = _secs / atc::SIXTY;
	int h =     m / atc::SIXTY;
		m =     m % atc::SIXTY;

	setText( QString().sprintf( FORMAT_TEXT, h,m,s ) );
}


////////////////////////////////////////////////////////////////////////////////////////////////////
